package com.hy.art;

import java.util.ArrayList;
import java.util.List;

// show tree structure as ascii format 
class Cord{
	public Cord(int x,int y,String text,boolean hasLeft,boolean hasRight) {
		this.x=x;
		this.y=y;
		this.text=text;
		this.hasLeft=hasLeft;
		this.hasRight=hasRight;
	}
	int x;
	int y;
	String text;
	boolean hasLeft;
	boolean hasRight;
}

public class AsciiTree {
	public AsciiTree(Node root) {
		root.x=0;
		root.y=0;
		setXY(root);
		
		List<Cord> posList=new ArrayList<Cord>();
		collectNodeCord(root,posList);
		
		int minX=999;
		int maxX=-1;
		int maxY=-1;
		for(Cord p:posList) {
			//System.out.println(" ("+p.x+","+p.y+")");
			if(p.x<minX) {
				minX=p.x;
			}
			
			if(p.x>maxX) {
				maxX=p.x;
			}
			
			if(p.y>maxY) {
				maxY=p.y;
			}
		}
		
		
		int width=maxX-minX+1;
		int height=maxY+1;
		//System.out.println("width="+width+" height="+height);
		
		String[][] arr = new String[width][height];
        // 瀵规暟缁勮繘琛屽垵濮嬪寲锛岄粯璁や负涓�涓┖鏍�
        for (int i = 0; i < width; i ++) {
            for (int j = 0; j < height; j ++) {
                arr[i][j] = " ";
            }
        }
        
        for(Cord p:posList) {
        	int x=p.x-minX;
        	int y=p.y;
        	
        	if(p.hasRight) {
        		if(x-2>=0 && y+1<height) {
            		arr[x-2][y+1]="鈹�";
            	}
        		
        		if(x-2>=0) {
            		arr[x-2][y]="鈹�";
            	}
        		
        		if(x-1>=0) {
            		arr[x-1][y]="|";
            	}
        	}
        	
        	
        	arr[x][y]=p.text;
        	
        	if(p.hasLeft) {
        		if(x+1<width) {
            		arr[x+1][y]="|";
            	}
        		
        		if(x+2<width) {
            		arr[x+2][y]="鈹�";
            	}
        		
        		if(x+2<width && y+1<height && y+1>=0) {
            		arr[x+2][y+1]="鈹�";
            	}
        	}
        }
        
        for (String[] line: arr) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < line.length; i ++) {
                sb.append(line[i]);
            }
            System.out.println(sb.toString());
        }
	}
	
	private void collectNodeCord(Node node,List<Cord> posList) {
		if(node.getLeftChild()!=null) {
			collectNodeCord(node.getLeftChild(),posList);
		}
		
		//System.out.println(node.getText()+" ("+node.x+","+node.y+")");
		posList.add(new Cord(node.x,node.y,node.getText(),node.getLeftChild()!=null,node.getRightChild()!=null));
		
		if(node.getRightChild()!=null) {
			collectNodeCord(node.getRightChild(),posList);
		}
	}
	
	private void setXY(Node node) {
		if(node.getLeftChild()!=null) {
			node.getLeftChild().x=node.x-2;
			node.getLeftChild().y=node.y+2;
			setXY(node.getLeftChild());
		}
		
		if(node.getRightChild()!=null) {
			node.getRightChild().x=node.x+2;
			node.getRightChild().y=node.y+2;
			setXY(node.getRightChild());
		}
	}
	
	public String getVTree() {
		return null;
	}
	
	public String getHTree() {
		return null;
	}
	
	public static void main(String[] args) {
		//System.out.println("鈹屸攢鈹�+鈹�鈹�鈹�");
		//System.out.println("鈺�     鈺�");
		//System.out.println("2     3");
		
		Node n1=new Node("1");
		Node n2=new Node("2");
		Node plus=new Node("+");
		plus.setLeftChild(n1);
		plus.setRightChild(n2);
		
		Node n3=new Node("3");
		Node n4=new Node("4");
		Node minus=new Node("-");
		minus.setLeftChild(n3);
		minus.setRightChild(n4);
		
		Node multi=new Node("*");
		multi.setLeftChild(plus);
		multi.setRightChild(minus);
		
		new AsciiTree(multi);
	}
}
